package ru.averta.notes.web.validators;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import ru.averta.notes.domain.forms.SiteAddForm;
import ru.averta.notes.other.UtilsValidator;

/**
 *
 * @author fedor.belov
 */

@Component
public class AddSiteValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return SiteAddForm.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        SiteAddForm site = (SiteAddForm) target;

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "title.empty");
        if(!UtilsValidator.isUrl(site.getUrl())) errors.rejectValue("url", "url.notCorrect");
    }

}
